﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using XrmLibrary.ConfigFile;
using XrmLibrary.Exceptions;

namespace XrmLibrary.Configurations
{
    public class AdapterConfiguration
    {
        private readonly CrmServiceConfigurationElement _configuration;
        private readonly DynamicInstanceConfiguration _dynamicConfiguration;
        private readonly Boolean _isDynamicallyConfigured;

        public AdapterConfiguration(ConfigurationElement configuration)
        {
            _configuration = (CrmServiceConfigurationElement)configuration;
            _isDynamicallyConfigured = false;
        }

        public AdapterConfiguration(DynamicInstanceConfiguration configuration)
        {
            _dynamicConfiguration = configuration;
            _isDynamicallyConfigured = true;
        }


        public CrmServiceConfigurationElement GetCrmServiceConfiguration()
        {
            if(_isDynamicallyConfigured)
            {
                throw new XrmLibraryException("The service was dynamically configured.  Use GetDynamicValue instead.");
            }
            return _configuration;
        }

        public String GetConfigurationName()
        {
            if(_isDynamicallyConfigured)
            {
                return _dynamicConfiguration.InstanceName;
            }
            else
            {
                return _configuration.Name;
            }
        }

        public String GetDynamicValue(String key)
        {
            if(!_isDynamicallyConfigured)
            {
                throw new XrmLibraryException("The adapter was not configured dynamically.");
            }
            return _dynamicConfiguration[key];
        }
    }
}
